'
' DotNetNukeŽ - http://www.dotnetnuke.com
' Copyright (c) 2002-2006
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'
Imports System.Web.Services.Protocols
Imports System.Diagnostics
Imports System.Reflection

Namespace DotNetNuke.Modules.IWeb
    ' class that is passed in through the soap header
    Public Class IWebAuthendicationHeader
        Inherits SoapHeader

        Public PortalID As Integer
        Public UserID As Integer
        Public Username As String
        Public Password As String
        Public Encrypted As String
    End Class

    Public Class IWebAuthendication
        ' const values
        Private Class Consts
            Public Const LoginErrorMessage As String = "Login failed. Invalid credentials supplied to the web service method."
        End Class

        Private _MethodName As String
        Private _PortalID As Integer
        Private _UserID As Integer
        Private _Username As String
        Private _Password As String
        Private _Encrypted As Boolean

#Region "Constructors"
        Public Sub New(ByVal PortalId As Integer, ByVal Username As String, ByVal Password As String, ByVal Encrypted As Boolean)

            Dim st As StackTrace = New StackTrace()
            Dim sf As StackFrame = st.GetFrame(1)
            Dim mb As MethodBase = sf.GetMethod()

            _MethodName = mb.Name
            _PortalID = PortalId
            _Username = Username
            _Encrypted = Encrypted
            _Password = GetPassword(Password)
        End Sub
        Public Sub New(ByVal IWebCredentials As IWebAuthendicationHeader)
            Dim st As StackTrace = New StackTrace()
            Dim sf As StackFrame = st.GetFrame(1)
            Dim mb As MethodBase = sf.GetMethod()

            _MethodName = mb.Name
            _PortalID = IWebCredentials.PortalID
            _Username = IWebCredentials.Username
            _Encrypted = IWebCredentials.Encrypted
            _Password = GetPassword(IWebCredentials.Password)
        End Sub
#End Region

        Public Function ValidAndAuthorized() As Boolean
            If _Password Is Nothing OrElse _Username Is Nothing Then
                Throw New SecurityException(Consts.LoginErrorMessage)
            End If

            If IsUserValid() Then
                Dim objUser As New UserInfo()
                objUser = GetUserInfo()
                Dim strAuthorizedRole As String = AuthorizedRole()
                Return IsAuthorized(objUser, strAuthorizedRole)
            Else
                Return False
            End If
        End Function

        Private Function IsUserValid() As Boolean
            Dim loginStatus As Security.Membership.UserLoginStatus

            ' If The password is encrypted un-encrypt it first
            If _Encrypted Then
                ' Get the Encryption Key
                Dim strEncryptionKey As String = EncryptionKey()
                ' Un-encrypt the password
                Dim objIWebEncryption As New IWebEncryption()
                _Password = objIWebEncryption.DeCrypt(_Password, strEncryptionKey)
            End If

            Dim objUser As UserInfo = UserController.ValidateUser(_PortalID, _Username, _Password, "", "", "255.255.255.1", loginStatus)
            If objUser Is Nothing Then
                Return False
            Else
                Return True
            End If
        End Function

        Private Function GetUserInfo() As UserInfo
            Dim loginStatus As Security.Membership.UserLoginStatus
            Return UserController.ValidateUser(_PortalID, _Username, _Password, "", "", "255.255.255.1", loginStatus)
        End Function

        Private Function AuthorizedRole() As String
            ' Find the authorized role for this method
            Dim strAuthorizedRole As String = ""

            Dim IWEBMethodInfo As New IWEBMethodInfo
            IWEBMethodInfo = IWEBMethods.GetMethodInDatabase(_PortalID, _MethodName)

            If Not IWEBMethodInfo.SecuritySetting = "" Then
                strAuthorizedRole = IWEBMethodInfo.SecuritySetting
            Else
                strAuthorizedRole = "Host"
            End If

            Return strAuthorizedRole
        End Function

        Private Function EncryptionKey() As String
            ' Find the authorized role for this portal
            Dim strEncryptionKey As String = ""

            strEncryptionKey = IWebController.GetIWebSettings(_PortalID, "SecurityKey")
            Return strEncryptionKey

        End Function

        Private Function IsAuthorized(ByVal UserInfo As UserInfo, ByVal strAuthorizedRole As String) As Boolean
            Dim RoleController As Security.Roles.RoleController = New Security.Roles.RoleController
            Dim strRoles As String() = RoleController.GetRolesByUser(UserInfo.UserID, UserInfo.PortalID)
            Dim strRole As String

            If UserInfo.IsSuperUser Then
                Return True
            End If

            For Each strRole In strRoles
                If strRole = strAuthorizedRole Then
                    Return True
                End If
            Next strRole

            Return False

        End Function

        Private Function GetPassword(ByVal parmPassword As String) As String
            Dim strPassword As String = ""
            If _Encrypted Then
                strPassword = HttpUtility.UrlDecode(parmPassword)
            Else
                strPassword = parmPassword
            End If
            Return strPassword
        End Function
    End Class

End Namespace
